﻿@model IEnumerable<BaseData.Model.InRecord>

@{
    ViewBag.Title = "进货记录";
}
@*@Html.Partial("~/Views/Shared/_GoodsTitle.cshtml")*@

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="row">
            <div class="col-xs-8 col-sm-8 col-md-6 col-lg-10">
                @using (Ajax.BeginForm("Index", new RouteValueDictionary { { "id", "" } }, new AjaxOptions { UpdateTargetId = "tables", HttpMethod = "Get", InsertionMode = InsertionMode.Replace }, new RouteValueDictionary { { "id", "searchForm" }, { "class", "bs-example bs-example-form" }, { "role", "form" } }))
                {
                    <div class="row">

                        <div class="col-lg-5 col-md-7">
                            <div class="input-group">
                                <input type="text" class="form-control" name="key" id="key" placeholder="查询条件：条码,名称,经办人">
                                <span class="input-group-btn">
                                    <button class="btn btn-default" type="submit">
                                        <span class="glyphicon glyphicon-search"></span>
                                    </button>

                                </span>

                            </div>
                        </div>

                    </div>
                }
            </div>
            <div class=" col-md-1 pull-right">
                <a href="#" data-toggle="modal" data-backdrop="static" data-target="#myModal" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-plus"></span> 新建</a>
            </div>
        </div>

    </div>
    <div class="panel-body">
        <div id="tables">
            @Html.Partial("_InRecordsSearchGet", Model)
        </div>


    </div>
</div>


<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">添加进货记录</h4>
            </div>
            <div class="modal-body">
                <div class=" form-horizontal">

                    <div class="form-group">
                        <label for="productcode" class="col-sm-2 control-label">商品条码</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="productcode"
                                   placeholder="请输商品条码(数字)" maxlength="50" onkeyup="value=value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="innum" class="col-sm-2 control-label">商品数量</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="innum"
                                   placeholder="请输入商品数量" onkeyup="value=value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
                        </div>
                    </div>



                    <div class="form-group">
                        <label for="DynamicPrice" class="col-sm-2 control-label">单品进价</label>
                        <div class="col-sm-10">
                            <div class="input-group">
                                <span class="input-group-addon">￥</span>
                                <input type="text" class="form-control" id="DynamicPrice" placeholder="请输入单品进价" onkeyup="value=value.replace(/[^\d/.]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d/.]/g,''))">
                                <span class="input-group-addon">元</span>
                            </div>
                        </div>
                    </div>

                </div>
            </div>

            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="errmsg">输出数据不合法，请检查重新输入！</div>
                <button type="button" class="btn btn-default" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
                <button id="save" type="button" class="btn btn-primary">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="emyModal" tabindex="-1" role="dialog" aria-labelledby="emyModalLabel" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="emyModalLabel">编辑进货记录</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="einrecordid" />
                <input type="hidden" id="ecreateat" />
                <input type="hidden" id="ecreateby" />
                <div class=" form-horizontal">
                    <div class="form-group">
                        <label for="eproductcode" class="col-sm-2 control-label">商品条码</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="eproductcode"
                                   placeholder="请输商品条码">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="einnum" class="col-sm-2 control-label">商品数量</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="einnum"
                                   placeholder="请输入商品数量" onkeyup="value=value.replace(/[^\d]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="eDynamicPrice" class="col-sm-2 control-label">单品进价</label>
                        <div class="col-sm-10">
                            <div class="input-group">
                                <span class="input-group-addon">￥</span>
                                <input type="text" class="form-control" id="eDynamicPrice" placeholder="请输入单品进价" onkeyup="value=value.replace(/[^\d/.]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d/.]/g,''))">
                                <span class="input-group-addon">元</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <div class="alert alert-danger" style="display:none" id="edrrmsg">输出数据不合法，请检查重新输入！</div>
                <button type="button" class="btn btn-default" data-dismiss="modal"><span style="cursor:pointer" class="glyphicon glyphicon-remove"></span>关闭</button>
                <button id="esave" type="button" class="btn btn-primary">
                    <span class="glyphicon glyphicon-ok"></span> 提交
                </button>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script>
        var authJson = null;
        $(function () {
            $("[data-toggle='tooltip']").tooltip();
            $("#productcode").bind('change', function () {
                $("#errmsg").css("display", "none")

                $.getJSON("/Products/CheckCode", { code: $(this).val() }, function (data) {
                    if (data != "ishave") {
                        $("#errmsg").css("display", "block").text("条码:" + $("#productcode").val() + " 在系统中未找到，请先到商品信息页面维护！");
                        $("#useraccount").val('').focus();
                    }
                });

            });

            $("#eproductcode").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#innum").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#einnum").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#DynamicPrice").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });
            $("#eDynamicPrice").bind('change', function () { $(this).parents(".form-group").attr("class", "form-group"); });

            $("#save").bind("click", function () {
                $("#errmsg").css("display", "none");
                var model = {};
                model.Num = $("#innum").val();
                model.ProductCode = $("#productcode").val();
                model.InPrice = $("#DynamicPrice").val();

                if (model.ProductCode == "") {
                    $("#productcode").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.Num == "") {
                    $("#innum").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.InPrice == "") {
                    $("#DynamicPrice").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }

                $.ajax({
                    url: "/InRecords/Create",
                    type: 'POST',
                    data: { jsonstr: JSON.stringify(model) },
                    success: function (data) {
                        if (data == "OK") {
                            location.href = "/InRecords/Index";
                        }
                    },
                    error: function (error) {
                        if (error.status == 500) {
                            $("#errmsg").css("display", "block");
                        }
                    }
                });
            });
            $("#esave").bind("click", function () {
                $("#ederrmsg").css("display", "none");
                var model = {};
                model.Num = $("#einnum").val();
                model.ProductCode = $("#eproductcode").val();
                model.InPrice = $("#eDynamicPrice").val();
                model.InRecordID = $("#einrecordid").val();
                model.CreateAt = $("#ecreateat").val();
                model.CreateBy = $("#ecreateby").val();

                if (model.Num == "") {
                    $("#einnum").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                if (model.InPrice == "") {
                    $("#eDynamicPrice").parents(".form-group").attr("class", "form-group has-error");
                    return;
                }
                $.ajax({
                    url: "/InRecords/Edit",
                    type: 'POST',
                    data: { jsonstr: JSON.stringify(model) },
                    success: function (data) {
                        if (data == "OK") {
                            location.href = "/InRecords/Index";
                        }
                    },
                    error: function (error) {
                        if (error.status == 500) {
                            $("#errmsg").css("display", "block");
                        }
                    }
                });
            });

        });
        function Edit(id) {
            $.ajax({
                url: "/InRecords/GetForEdit",
                type: 'GET',
                data: { id: id },
                success: function (data) {
                    $("#eproductcode").val(data.ProductCode).attr("disabled", "disabled");
                    $("#einnum").val(data.Num);
                    $("#eDynamicPrice").val(data.InPrice);
                    $("#einrecordid").val(data.InRecordID);
                    $("#ecreateat").val(data.CreateAt);
                    $("#ecreateby").val(data.CreateBy);
                    $('#emyModal').modal('show');
                }
            });


        }
        function Delete(id) {
            if (confirm("提示：以下操作将会删除该商品进货记录，是否继续?")) {
                $.ajax({
                    url: "/InRecords/Delete",
                    type: 'GET',
                    data: { id: id },
                    success: function (data) {
                        location.href = "/InRecords/Index";
                    }
                });
            }
        }
    </script>
}